package cn.edu.scau.cmi.chenmiaopeng.hibernateFrame.client;

import java.io.IOException;
import java.util.List;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
import cn.edu.scau.cmi.chenmiaopeng.controller.Hibernate.*;
import cn.edu.scau.cmi.chenmiaopeng.hibernate.dao.TeacherDAO;

public class HibernateFrameWorkClient extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	public void start(Stage primaryStage) throws Exception{
		try {
		  FXMLLoader loader = new FXMLLoader(getClass().getResource("/cn/edu/scau/cmi/chenmiaopeng/ui/Hibernate/HibernateUI.fxml"));
		  
		  Parent root = loader.load();
		  
          Scene scene = new Scene(root, 764, 551);
		  
		  primaryStage.setScene(scene);
		  
		  primaryStage.setTitle("Hibernate框架");
		  
		  initView(loader);
		  
		  primaryStage.show();
		}
		
		catch(IOException e) {
			e.printStackTrace();
		}
	}
	public void initView(FXMLLoader loader) {
		ToggleGroup tg = new ToggleGroup();
		RadioButton radio1 = (RadioButton)loader.getNamespace().get("radio1");
		RadioButton radio2 = (RadioButton)loader.getNamespace().get("radio2");
		radio1.setToggleGroup(tg);
		radio2.setToggleGroup(tg);
		radio1.setSelected(true);
		radio1.setUserData("teacher");
		radio2.setUserData("student");
//		StudentDAO studentDAO = new StudentDAO();
//		studentDAO.
		TeacherDAO teacherDAO = new TeacherDAO();
		List teacherList = teacherDAO.findAll();
		//初始化老师下拉框
		ComboBox combobox1 = (ComboBox)loader.getNamespace().get("combobox1");
		ComboBox combobox2 = (ComboBox)loader.getNamespace().get("combobox2"); 
		
		ObservableList<?> teachers = FXCollections.observableArrayList(teacherList);
		combobox2.setItems(teachers);
		combobox2.setPromptText("请选择老师:");
		//初始化老师功能下拉框
		ObservableList<?> t_fun = FXCollections.observableArrayList("添加老师","仅修改老师名字","修改老师工号和名字","删除老师","添加指导学生","修改指导学生","删除指导学生","显示所有老师");
		combobox1.setItems(t_fun);
		combobox1.setPromptText("请选择功能:");
		//给单选按钮增加监听器：
		HibernateFrameController hibernateController = loader.getController();  //直接从loader里面getController()可行
		
		//给下拉框添加监听器：
		hibernateController.addComboBoxListener();
		
		hibernateController.setToggle(tg);
		hibernateController.addToggleListener();
//		loader.setController(hibernateController);  //这样可以在loader.load()之前设置controller,保证controller一致
	}
}
